﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using AutoCreateMapAndRoad.MapFolder;
using Microsoft.Xna.Framework.Input.Touch;

namespace AutoCreateMapAndRoad
{
    public partial class GamePage : PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;
        public static float scale;
        public static Vector2 DoLech;
        Map map;
        Vector2 start; // vị trí bắt đầu chạm
        Vector2 curtouch; // vị trí chạm hiện tại
        SpriteFont font;
        public GamePage()
        {
            InitializeComponent();

            // Get the content manager from the application
            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(333333);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
            scale = 0.25f;
            DoLech = new Vector2(0, 0);
            map = new Map(contentManager);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            // TODO: use this.content to load your game content here
            map.Load(contentManager);
            font = contentManager.Load<SpriteFont>("fonts/font");
            // Start the timer
            timer.Start();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }

        /// <summary>
        /// Allows the page to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            // TODO: Add your update logic here
            TouchCollection touchs = TouchPanel.GetState();
            foreach (TouchLocation touch in touchs)
            {
                if (touch.State == TouchLocationState.Pressed)
                {
                    start = touch.Position;
                    curtouch = touch.Position;
                }
                if (touch.State == TouchLocationState.Moved)
                {
                    DoLech += touch.Position - curtouch;
                    curtouch = touch.Position;
               //     if (DoLech.X > 0) DoLech.X = 0;
                 //   if (DoLech.Y > 0) DoLech.Y = 0;
                   // if (-DoLech.X + 800 > Map.SizeOfMatrix.Y * 128 * scale) DoLech.X = 800 - Map.SizeOfMatrix.Y * 128 * scale;
                    //if (-DoLech.Y + 480 > Map.SizeOfMatrix.X * 128 * scale) DoLech.Y = 480 - Map.SizeOfMatrix.X * 128 * scale;
                }
                if (touch.State == TouchLocationState.Released)
                {
                    curtouch = touch.Position;
                    DoLech = new Vector2(0, 0);
                    if (Vector2.Distance(start, curtouch) > 300)
                    {
                        map = new Map(contentManager);
                        map.Load(contentManager);
                    }
                }
            }
        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            map.Draw(spriteBatch);
            spriteBatch.DrawString(font, "số hàng  :" + Map.SizeOfMatrix.X, new Vector2(0, 30), Color.White);
            spriteBatch.DrawString(font, "số cột  :" + Map.SizeOfMatrix.Y, new Vector2(0, 70), Color.White);
            //spriteBatch.DrawString(font, "nước đi tối thiểu  :" + map.getBuocDi(), new Vector2(0, 110), Color.White);
            spriteBatch.End();
        }
    }
}